<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var arr = [1, 2, 3, 4]
        var total = arr.reduce((prev, current, index, arr) => {
            console.log(prev, current, index, arr)
            return prev + current
        }, 1)
        console.log(total)

        Array.prototype._reduce = function (fn, prev) {
            var i = 0;
            // 这里面this指向arr
            if (typeof fn !== 'function') {
                throw new Error('第一个参数必须是函数')
            }
            if (typeof prev === 'undefined') {
                prev = this[0]
                i = 1
            }

            for (; i < this.length; i++) {
                prev = fn(prev, arr[i], i, this)
            }

            return prev
        }

        var total2 = arr._reduce((prev, current, index, arr) => {
            console.log(prev, current, index, arr)
            return prev + current
        }, 1)
        console.log(total2)

    </script>
</body>

</html>